package com.funny.learning.english.api;

import java.util.HashMap;
import java.util.List;

import org.apache.http.cookie.Cookie;

import android.app.ProgressDialog;
import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.ContextThemeWrapper;

import com.androidquery.callback.AjaxCallback;
import com.androidquery.callback.AjaxStatus;
import com.funny.learning.english.LeaningEnglishAppication;
import com.funny.learning.english.model.UserEntity;

//import com.funny.learning.english.util.Config;

public class BaseAjaxCallBack<T> extends AjaxCallback<T> {

	protected static final HashMap<String, String> sCookies = new HashMap<String, String>();

	protected IAjaxCallbackMMK mAjaxCallbackMMK;

	private ProgressDialog mProgressDialog;

	private boolean mShowDialogAble = true;

	public void setIAjaxCallbackMMK(IAjaxCallbackMMK ajaxCallbackMMK) {
		mAjaxCallbackMMK = ajaxCallbackMMK;
	}

	public void setShowDialogAble(boolean condition) {
		mShowDialogAble = condition;
	}

	@Override
	public void async(Context context) {

		LeaningEnglishAppication leaningEnglishAppication = LeaningEnglishAppication
				.getLeaningEnglishAppication();
		
		 

		UserEntity userEntity = leaningEnglishAppication.getUserEntity();

		if (null != userEntity) {
			
			String a = "llllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllll";

			Log.d("zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz", a.length()+"");
			if (!TextUtils.isEmpty(userEntity.getUhash())) {
				sCookies.put("_cl_uhash", userEntity.getUhash());
				Log.d("+++++++++++++++++++++", "OKKKKKKKKKKKKKKKKKKKKKKKKK");
			}

			if (!TextUtils.isEmpty(userEntity.getToken())) {
				sCookies.put("_cl_uname", userEntity.getName());
				Log.d("+++++++++++++++++++++", "OKKKKKKKKKKKKKKKKKKKKKKKKK");
			}

			if (!TextUtils.isEmpty(userEntity.getToken())) {
				sCookies.put("_cl_uiid", userEntity.getIid() + "");
				Log.d("+++++++++++++++++++++", "OKKKKKKKKKKKKKKKKKKKKKKKKK");
			}

			if (!TextUtils.isEmpty(userEntity.getToken())) {
				sCookies.put("_cl_uid", userEntity.getId());
				Log.d("+++++++++++++++++++++", "OKKKKKKKKKKKKKKKKKKKKKKKKK");
			}

			if (!TextUtils.isEmpty(userEntity.getToken())) {
				sCookies.put("_cl_token", userEntity.getToken());
				Log.d("+++++++++++++++++++++", "OKKKKKKKKKKKKKKKKKKKKKKKKK");
			}

			if (sCookies.containsKey("PHPSESSID")) {
				Log.d("+++++++++++++++++++++", "FUCKKKKKKKKKKKKKKKKKk"); 
			}

			cookies(sCookies);

			Log.d("+++++++++++++++++++++", "OKKKKKKKKKKKKKKKKKKKKKKKKK");
		}

		if (mProgressDialog == null) {
			mProgressDialog = new ProgressDialog(new ContextThemeWrapper(
					context, android.R.style.Theme_Holo_Light_Dialog));
			mProgressDialog.setCanceledOnTouchOutside(false);
			mProgressDialog.setCancelable(true);
			// mProgressDialog.setOnCancelListener(new OnCancelListener() {
			//
			// @Override
			// public void onCancel(DialogInterface dialog) {
			// mAjaxCallbackMMK.onBackDialog();
			//
			// }
			// });
			mProgressDialog.setMessage("Đang tải...");
		}

		if (mShowDialogAble)
			mProgressDialog.show();

		if (null != mAjaxCallbackMMK) {
			mAjaxCallbackMMK.handler(null, IAjaxCallbackMMK.START);
		}

		super.async(context);
	}

	@Override
	public void callback(String url, T object, AjaxStatus status) {
		// TODO Auto-generated method stub
		super.callback(url, object, status);

		List<Cookie> cookies = status.getCookies();

		if (!sCookies.containsKey("PHPSESSID"))
			for (int i = 0; i < cookies.size(); ++i) {
				Cookie cookie = cookies.get(i);
//				sCookies.put(cookie.getName(), cookie.getValue());
				Log.d(cookie.getName() + " ", cookie.getValue());

				if ("PHPSESSID".equals(cookie.getName())) {
					sCookies.put("PHPSESSID", cookie.getValue());
				}
			}

		mProgressDialog.dismiss();

		if (null == object) {
			if (null != mAjaxCallbackMMK) {
				mAjaxCallbackMMK.handler(null, IAjaxCallbackMMK.ERROR);
			}
			return;
		}
	}

	public interface IAjaxCallbackMMK {
		public int START = 0;
		public int ERROR = -1;
		public int COMPLETE = 1;

		public <T> void handler(T t, int code);

	}

}
